﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UtilLib
{
    public static class DateTimeUtil
    {
        public static DateTime ConvertToDateTime(Int64 ticks)
        {
            Int64 _tk = new DateTime(1970, 1, 1, 0, 0, 0).Ticks + ticks * 10000;
            return new DateTime(_tk).ToLocalTime();
        }
        public static Int64 ConvertToTicks(DateTime date)
        {
            Int64 _tk = date.ToUniversalTime().Ticks - new DateTime(1970, 1, 1, 0, 0, 0).Ticks;
            return _tk / 10000;
        }
        /// <summary>
        /// Converts the int to date time.
        /// </summary>
        /// <param name="ticks">The ticks.</param>
        /// <returns></returns>
        public static DateTime? ConvertIntToDateTime(Int32 ticks)
        {
            if (ticks == 0)
                return null;
            DateTime date = new DateTime(ticks / 10000, ticks % 10000 / 100, ticks % 100);
            return date;
        }

        /// <summary>
        /// Converts the date time to int.
        /// </summary>
        /// <param name="date">The date.</param>
        /// <returns></returns>
        public static Int32? ConvertDateTimeToInt(DateTime? date)
        {
            if (date == null)
                return null;
            Int32 _tk = Int32.Parse(date.Value.ToString("yyyyMMdd"));
            return _tk;
        }
    }
}
